//
//  BTEPrivicyView.swift
//  BTE
//
//  Created by 张文 on 2025/8/16.
//


import UIKit
import SnapKit


class PrivacyAgreementView: UIView {
    
    // MARK: - Callbacks
    var didToggleCheckbox: ((Bool) -> Void)?
    
    var didTapPrivacyLink: (() -> Void)?
    
    // MARK: - UI Components
    lazy var checkboxButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "Group 1171276716"), for: .selected)
        button.setImage(UIImage(named: "Ellipse 40"), for: .normal)
        button.addTarget(self, action: #selector(handleCheckboxTap), for: .touchUpInside)
        button.isSelected = true
        return button
    }()
    
    private lazy var agreementTextView: UITextView = {
        let textView = UITextView()
        textView.isEditable = false
        textView.isScrollEnabled = false
        textView.backgroundColor = .clear
        textView.textContainerInset = .zero
        textView.textContainer.lineFragmentPadding = 0
        textView.linkTextAttributes = [
            .foregroundColor: UIColor.systemBlue,
            .underlineStyle: NSUnderlineStyle.single.rawValue
        ]
        textView.delegate = self
        textView.isUserInteractionEnabled = true
        return textView
    }()
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupSubviews()
    }
    
    // MARK: - Setup
    private func setupSubviews() {
        addSubview(checkboxButton)
        addSubview(agreementTextView)
        
        checkboxButton.snp.makeConstraints { make in
            make.leading.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }
        
        agreementTextView.snp.makeConstraints { make in
            make.leading.equalTo(checkboxButton.snp.trailing).offset(8)
            make.trailing.top.bottom.equalToSuperview()
            make.centerY.equalTo(checkboxButton)
        }
    }
    
    // MARK: - Configuration
    func configure(withText staticText: String, linkText: String, fontSize: CGFloat = 12) {
        let fullText = "\(staticText) \(linkText)"
        let attributedString = NSMutableAttributedString(string: fullText)
        
        // Configure static text
        let staticTextRange = NSRange(location: 0, length: staticText.count + 1)
        attributedString.addAttributes([
            .font: UIFont.systemFont(ofSize: fontSize),
            .foregroundColor: UIColor.label
        ], range: staticTextRange)
        
        // Configure link text
        let linkRange = NSRange(location: staticText.count + 1, length: linkText.count)
        attributedString.addAttributes([
            .font: UIFont.systemFont(ofSize: fontSize),
            .link: "privacy://"
        ], range: linkRange)
        
        agreementTextView.attributedText = attributedString
    }
    
    // MARK: - Actions
    @objc private func handleCheckboxTap() {
        checkboxButton.isSelected.toggle()
        didToggleCheckbox?(checkboxButton.isSelected)
    }
}

// MARK: - UITextViewDelegate
extension PrivacyAgreementView: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        if url.scheme == "privacy" {
            didTapPrivacyLink?()
            return false
        }
        return true
    }
}
